PowerTCP SSH and SFTP for .NET
Read(Byte[],Int32,Int32) Method
Example 




Buffer where data is to be copied to.
Offset into the buffer.
Number of bytes available in buffer starting at the offset.
Reads session data provided by the SSH-2 host.
Syntax
'Declaration
 
Public Overloads Overrides Function Read( _
   ByVal buffer() As Byte, _
   ByVal offset As Integer, _
   ByVal count As Integer _
) As Integer
'Usage
 
Dim instance As SessionStream
Dim buffer() As Byte
Dim offset As Integer
Dim count As Integer
Dim value As Integer
 
value = instance.Read(buffer, offset, count)
public override int Read( 
   byte[] buffer,
   int offset,
   int count
)
public: int Read( 
   byte[]* buffer,
   int offset,
   int count
) override 
public:
int Read( 
   array<byte>^ buffer,
   int offset,
   int count
) override 

Parameters

buffer
Buffer where data is to be copied to.
offset
Offset into the buffer.
count
Number of bytes available in buffer starting at the offset.

Return Value

The number of bytes read. Returns 0 if the server has closed the channel.
Remarks
This method always blocks, and is normally invoked from a worker thread.
Example
The example below demonstrates how to receive data without blocking the UI.
private void button1_Click(object sender, System.EventArgs e)
{
    //Connect and log into the server. Can be executed on a worker 
    //thread to not block the UI; executed synchronously here for simplicity
    connectAndLogin(myServerHostname, myServerPort, myUsername, myPassword);
    //Execute the specified 'receiveData' method on a worker thread to not block the UI
    ssh1.Start(receiveData, null);
}

private void connectAndLogin(string hostname, int port, string username, string password)
{
    //Connect to the server
    ssh1.Connection.RemoteEndPoint.HostNameOrAddress = hostname;
    ssh1.Connection.RemoteEndPoint.Port = port; //Usually 22
    ssh1.Connect();

    //Authenticate
    SshLoginData loginDetails = new SshLoginData();
    loginDetails.Username = username;
    loginDetails.Password = password;
    ssh1.Authenticate(loginDetails);
}

/// <summary>
/// Receives data from the server and marshals it to the UI thread for display.
/// </summary>
/// <param name="notUsed">Not used in this snippet</param>
private void receiveData(object notUsed)
{
    //Establish a shell
    SessionStream session = ssh1.StartShell();

    //Receive data when it is sent by the remote host
    //Marshal it to the UI for display
    while (true)
    {
        byte[] buf = new byte[1024];
        Data data = session.Read(buf);
        //If data.Count is 0, the connection has been closed, so break out of the loop.
        if (data.Count == 0)
            break;
        ssh1.Marshal(data, session, "", null);
    }
}

/// <summary>
/// Raised on the UI thread, when ssh1.Marshal is called in receiveData.
/// </summary>
private void ssh1_Data(object sender, SshDataEventArgs e)
{
    textBox1.AppendText(e.Data.ToString());
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    'Connect and log into the server. Can be executed on a worker 
    'thread to not block the UI; executed synchronously here for simplicity
    connectAndLogin(myServerHostname, myServerPort, myUsername, myPassword)
    'Execute the specified 'receiveData' method on a worker thread to not block the UI
    ssh1.Start(AddressOf receiveData, Nothing)
End Sub

Private Sub connectAndLogin(ByVal hostname As String, ByVal port As Integer, ByVal username As String, ByVal password As String)
    'Connect to the server
    ssh1.Connection.RemoteEndPoint.HostNameOrAddress = hostname
    ssh1.Connection.RemoteEndPoint.Port = port 'Usually 22
    ssh1.Connect()

    'Authenticate
    Dim loginDetails As New SshLoginData()
    loginDetails.Username = username
    loginDetails.Password = password
    ssh1.Authenticate(loginDetails)
End Sub

''' <summary>
''' Receives data from the server and marshals it to the UI thread for display.
''' </summary>
''' <param name="notUsed">Not used in this snippet</param>
Private Sub receiveData(ByVal notUsed As Object)
    'Establish a shell
    Dim session As SessionStream = ssh1.StartShell()

    'Receive data when it is sent by the remote host
    'Marshal it to the UI for display
    Do
        Dim buf(1023) As Byte
        Dim data As Data = session.Read(buf)
        'If data.Count is 0, the connection has been closed, so break out of the loop.
        If data.Count = 0 Then
            Exit Do
        End If
        ssh1.Marshal(data, session, "", Nothing)
    Loop
End Sub

''' <summary>
''' Raised on the UI thread, when ssh1.Marshal is called in receiveData.
''' </summary>
Private Sub ssh1_Data(ByVal sender As Object, ByVal e As SshDataEventArgs) Handles ssh1.Data
    textBox1.AppendText(e.Data.ToString())
End Sub
See Also

Reference

SessionStream Class
SessionStream Members
Overload List


PowerTCP SSH and SFTP for .NET Documentation Version 7.0
© 2023 Dart Communications. All Rights Reserved.
Send comments on this topic